package loadsave;

import java.util.*;

import loadsave.loadsaveobject.*;

public class Index implements LoadSaveable {
	int[] indices;
	
	public Index() {
		indices = new int[0];
	}
	
	public Index(int ... newIndices) {
		indices = Arrays.copyOf(newIndices, newIndices.length);
	}
	
	public void addIndex(int index) {
		int[] newIndices = new int[indices.length + 1];
		System.arraycopy(indices, 0, newIndices, 0, indices.length);
		newIndices[indices.length] = index;
		indices = newIndices;
	}
	
	public int getIndex(int dimension) {
		return indices[dimension];
	}
	
	public List<Integer> getIndexList() {
		List<Integer> toReturn = new ArrayList<Integer>();
		for (int i = 0; i < indices.length; ++i)
			toReturn.add(indices[i]);
		return toReturn;
	}
	
	@Override
	public Memento getMemento() {
		return new Memento() {

			@Override
			public LoadSaveObject getState() {
				return new LoadSaveIndex(indices);
			}

			@Override
			public void restoreState(LoadSaveObject lso) {
				if (!lso.getType().equals("Index"))
					throw new RuntimeException("This LoadSaveObject does not represent an Index");
				LoadSaveIndex lsi = (LoadSaveIndex) lso;
				List<Integer> l = lsi.getIndices();
				indices = new int[l.size()];
				for (int i = 0; i < indices.length; ++i)
					indices[i] = l.get(i); 
			}
			
		};
	}

}
